#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
 // Number of steps per output rotation
const int stepsPerRevolution = 200;

 // in loop:
 // 200 full step 350
 // 100 half step 180
 // 50 quarter step 90

 // 1 step = 1.8

 // Dirction
 // HIGH for clockwise
 // LOW for anticlockwise

  //Speed: delayMicroseconds

const int stepPinRight = 22;
const int dirPinRight = 23;

const int stepPinLeft = 24;
const int dirPinLeft = 25;

const int stepPinDown = 26;
const int dirPinDown = 27;
 
const int stepPinFront = 28;
const int dirPinFront = 29;
 
const int stepPinBack = 30;
const int dirPinBack = 31;

int buttonStatus = 0;
int led = 13;
int str_len = 0;
int custom = 0;
int i = 0;
int customCounter = 0;
char *name = NULL;
String data;
int counter = 3;
int counter2 = 0;
int timer = 0;


void setup() {
  pinMode(10 , INPUT_PULLUP);
   // Sets the two pins as Outputs
  pinMode(stepPinRight, OUTPUT);
  pinMode(dirPinRight, OUTPUT);

    pinMode(stepPinLeft, OUTPUT);
   pinMode(dirPinLeft, OUTPUT);
   
    pinMode(stepPinDown, OUTPUT);
    pinMode(dirPinDown, OUTPUT);
   
    pinMode(stepPinFront, OUTPUT);
    pinMode(dirPinFront, OUTPUT);
   
    pinMode(stepPinBack, OUTPUT);
    pinMode(dirPinBack, OUTPUT);
  lcd.init();
  lcd.clear();
  lcd.backlight();
  Serial.begin(9600);
}

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

 // initialize an instance of class NewKeypad

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void loop() {
    lcd.print(counter);
      delay(1000);
  char customKey = customKeypad.getKey();

  if (customKey) {
    Serial.println(customKey);
    custom = customKey - '0';
    delay(100);
  }
  int pinValue = digitalRead(10);
  delay(500);  // quick and dirty debounce filter
        if (buttonStatus == pinValue) {
          Serial.write("v");
         pinValue = 1;
        }
  if (Serial.available() > 0) {
    data = Serial.readStringUntil('\n');
    Serial.println("You sent me: ");
    Serial.println(data);
         int pinValue = digitalRead(10);
         Serial.println(pinValue);
    delay(500);  // quick and dirty debounce filter
  }
  if (custom == 0 and buttonStatus == pinValue) {
    str_len = data.length();
    char char_array[str_len];
    data.toCharArray(char_array, str_len);
    name = strtok(char_array, " ");
    Serial.println("loop 1");

    while (name != NULL)
    {

      while (counter >= 1) {
        Serial.println(counter);
        lcd.setCursor(7, 0);  // Set cursor to character 2 on line 0
        lcd.print(counter);
        delay(1000);
        counter -= 1;
        lcd.clear();
      }

      lcd.setCursor(5, 0);  // Set cursor to character 2 on line 0
      lcd.print("Start!");
      Serial.println(name);
      if (strcmp(name, "R") == 0) {
        Serial.println("hello R");
        digitalWrite(dirPinRight, HIGH);//   with clockwise
        for (int x = 0 ; x < 50 ; x++) {
          digitalWrite(stepPinRight, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPinRight, LOW);
          delayMicroseconds(1000);
        }
        delay(500);
        lcdPrint(500);
      }
      else if (strcmp(name, "R'") == 0) {
        Serial.println("hello R'");
        digitalWrite(dirPinRight, LOW); //  with anticlockwise
        for (int x = 0 ; x < 50 ; x++) {
          digitalWrite(stepPinRight, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPinRight, LOW);
          delayMicroseconds(1000);
        }
        delay(500);
        lcdPrint(500);
      }
      else if (strcmp(name, "R2") == 0) {
        Serial.println("hello R2");
        digitalWrite(dirPinRight, HIGH); //  with clockwise
        for (int x = 0 ; x < 100 ; x++) {
          digitalWrite(stepPinRight, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPinRight, LOW);
          delayMicroseconds(1000);
        }
        delay(500);
        lcdPrint(500);
      }
     //   *****************************************************
             else if (strcmp(name, "F") == 0) {
               Serial.println("hello F");
               digitalWrite(dirPinFront, HIGH);  // with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinFront, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinFront, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "F'") == 0) {
               Serial.println("hello F'");
               digitalWrite(dirPinFront, LOW);  // with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinFront, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinFront, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "F2") == 0) {
               Serial.println("hello F2");
               digitalWrite(dirPinFront, HIGH); //  with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinFront, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinFront, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             //  *****************************************************
             else if (strcmp(name, "L") == 0) {
               Serial.println("hello L");
               digitalWrite(dirPinLeft, HIGH); //  with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinLeft, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinLeft, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "L'") == 0) {
               Serial.println("hello L'");
               digitalWrite(dirPinLeft, LOW); //  with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinLeft, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinLeft, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "L2") == 0) {
               Serial.println("hello L2");
               digitalWrite(dirPinLeft, HIGH); //  with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinLeft, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinLeft, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
           //    *****************************************************
             else if (strcmp(name, "B") == 0) {
               Serial.println("hello B");
               digitalWrite(dirPinBack, HIGH); //  with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinBack, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinBack, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "B'") == 0) {
               Serial.println("hello B'");
               digitalWrite(dirPinBack, LOW); //  with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinBack, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinBack, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "B2") == 0) {
               Serial.println("hello B2");
               digitalWrite(dirPinBack, HIGH); //  with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinBack, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinBack, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
            //   *****************************************************
             else if (strcmp(name, "D") == 0) {
               Serial.println("hello D");
               digitalWrite(dirPinDown, HIGH); //  with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinDown, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinDown, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "D'") == 0) {
               Serial.println("hello D'");
               digitalWrite(dirPinDown, LOW); //  with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinDown, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinDown, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "D2") == 0) {
               Serial.println("hello D2");
               digitalWrite(dirPinDown, HIGH); //  with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinDown, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinDown, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
      //  *****************************************************
      else if (strcmp(name, "U") == 0) {
        Serial.println("hello U");
        upClock();
      }
      else if (strcmp(name, "U'") == 0) {
        Serial.println("hello U'");
        upCounterClock();
      }
      else if (strcmp(name, "U2") == 0) {
        Serial.println("hello U2");
        upClock();
        upClock();
      }
      name = strtok(NULL, " ");
    }
    lcd.clear();
    lcd.setCursor(5, 0);  // Set cursor to character 2 on line 0
    lcd.print("Finish!");
    pinValue = 1;
  }
  else if ( custom != 0 and buttonStatus == pinValue) {
    str_len = data.length();
    char char_array[str_len];
    data.toCharArray(char_array, str_len);
    name = strtok(char_array, " ");
    while (name != NULL and customCounter < custom)
    {
      Serial.println(name);
      if (strcmp(name, "R") == 0) {
        Serial.println("hello R");
        digitalWrite(dirPinRight, HIGH);  // with clockwise
        for (int x = 0 ; x < 50 ; x++) {
          digitalWrite(stepPinRight, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPinRight, LOW);
          delayMicroseconds(1000);
        }        delay(500);
        lcdPrint(500);
      }
      else if (strcmp(name, "R'") == 0) {
        Serial.println("hello R'");
        digitalWrite(dirPinRight, LOW);  // with anticlockwise
        for (int x = 0 ; x < 50 ; x++) {
          digitalWrite(stepPinRight, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPinRight, LOW);
          delayMicroseconds(1000);
        }        delay(500);
        lcdPrint(500);
      }
      else if (strcmp(name, "R2") == 0) {
        Serial.println("hello R2");
        digitalWrite(dirPinRight, HIGH); //  with clockwise
        for (int x = 0 ; x < 100 ; x++) {
          digitalWrite(stepPinRight, HIGH);
          delayMicroseconds(1000);
          digitalWrite(stepPinRight, LOW);
          delayMicroseconds(1000);
        }        delay(500);
        lcdPrint(500);
      }
     //   *****************************************************
             else if (strcmp(name, "F") == 0) {
               Serial.println("hello F");
               digitalWrite(dirPinFront, HIGH);  // with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinFront, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinFront, LOW);
                 delayMicroseconds(1000);
               }        delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "F'") == 0) {
               Serial.println("hello F'");
               digitalWrite(dirPinFront, LOW);//   with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinFront, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinFront, LOW);
                 delayMicroseconds(1000);
               }        delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "F2") == 0) {
               Serial.println("hello F2");
               digitalWrite(dirPinFront, HIGH);//   with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinFront, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinFront, LOW);
                 delayMicroseconds(1000);
               }        delay(500);
               lcdPrint(500);
             }
            //   *****************************************************
             else if (strcmp(name, "L") == 0) {
               Serial.println("hello L");
               digitalWrite(dirPinLeft, HIGH);  // with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinLeft, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinLeft, LOW);
                 delayMicroseconds(1000);
       
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "L'") == 0) {
               Serial.println("hello L'");
               digitalWrite(dirPinLeft, LOW); //  with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinLeft, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinLeft, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "L2") == 0) {
               Serial.println("hello L2");
               digitalWrite(dirPinLeft, HIGH); //  with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinLeft, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinLeft, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
           //    *****************************************************
             else if (strcmp(name, "B") == 0) {
               Serial.println("hello B");
               digitalWrite(dirPinBack, HIGH); //  with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinBack, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinBack, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "B'") == 0) {
               Serial.println("hello B'");
               digitalWrite(dirPinBack, LOW);  // with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinBack, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinBack, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "B2") == 0) {
               Serial.println("hello B2");
               digitalWrite(dirPinBack, HIGH); //  with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinBack, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinBack, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
            //   *****************************************************
             else if (strcmp(name, "D") == 0) {
               Serial.println("hello D");
               digitalWrite(dirPinDown, HIGH);  // with clockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinDown, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinDown, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "D'") == 0) {
               Serial.println("hello D'");
               digitalWrite(dirPinDown, LOW);  // with anticlockwise
               for (int x = 0 ; x < 50 ; x++) {
                 digitalWrite(stepPinDown, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinDown, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
             else if (strcmp(name, "D2") == 0) {
               Serial.println("hello D2");
               digitalWrite(dirPinDown, HIGH);  // with clockwise
               for (int x = 0 ; x < 100 ; x++) {
                 digitalWrite(stepPinDown, HIGH);
                 delayMicroseconds(1000);
                 digitalWrite(stepPinDown, LOW);
                 delayMicroseconds(1000);
               }
               delay(500);
               lcdPrint(500);
             }
     //   *****************************************************
             else if (strcmp(name, "U") == 0) {
               Serial.println("U");
               upClock();
             }
             else if (strcmp(name, "U'") == 0) {
               Serial.println("U'");
               upCounterClock();
             }
             else if (strcmp(name, "U2") == 0) {
               Serial.println("hello U2");
               upClock();
               upClock();
             }
      name = strtok(NULL, " ");
      customCounter += 1;
    }
    lcd.clear();
    lcd.setCursor(5, 0); //  Set cursor to character 2 on line 0
    lcd.print("Finish!");
    custom = 0;
    pinValue = 1;
         delay(100000);
  }
}

 void upClock() {
   Serial.println("hello U");// R L F2 B2 R' L' D R L F2 B2 R' L'
 
 digitalWrite(dirPinRight,HIGH); //  with clockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinRight,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinRight,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinLeft,HIGH); //  with clockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinLeft,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinLeft,LOW);
       delayMicroseconds(1000);
 
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinFront,HIGH); //  with clockwise
     for(int x=0 ; x<100 ; x++){
       digitalWrite(stepPinFront,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinFront,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
  digitalWrite(dirPinBack,HIGH); //  with clockwise
     for(int x=0 ; x<100 ; x++){
       digitalWrite(stepPinBack,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinBack,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
  digitalWrite(dirPinRight,LOW);//   with anticlockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinRight,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinRight,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinLeft,LOW);  // with anticlockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinLeft,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinLeft,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinDown,HIGH);//   with clockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinDown,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinDown,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinRight,HIGH); //  with clockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinRight,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinRight,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinLeft,HIGH); //  with clockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinLeft,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinLeft,LOW);
       delayMicroseconds(1000);
 
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinFront,HIGH); //  with clockwise
     for(int x=0 ; x<100 ; x++){
       digitalWrite(stepPinFront,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinFront,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinBack,HIGH);  // with clockwise
     for(int x=0 ; x<100 ; x++){
       digitalWrite(stepPinBack,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinBack,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinRight,LOW); //  with anticlockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinRight,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinRight,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
 
 digitalWrite(dirPinLeft,LOW);//   with anticlockwise
     for(int x=0 ; x<50 ; x++){
       digitalWrite(stepPinLeft,HIGH);
       delayMicroseconds(1000);
       digitalWrite(stepPinLeft,LOW);
       delayMicroseconds(1000);
     }  delay(500);
   lcdPrint(500);
   Serial.println("end U");
 }

 void upCounterClock() {
   Serial.println("hello U'");// R2 L2 D F2 * B2 D F2 R2 F2 B2 D2 L2 D2 L2 R2 D2 R2 D2 R2 F2 D' R2 B2 R2 L2 F2 L2 D B2 F2 D
 
   digitalWrite(dirPinRight, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinLeft, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinLeft, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinLeft, LOW);
     delayMicroseconds(1000);
 
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, HIGH); //  with clockwise
   for (int x = 0 ; x < 50 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinFront, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinFront, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinFront, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
 //    B2 D F2 R2 * F2 B2 D2 L2 D2 L2 R2 D2 R2 D2 R2 F2 D' R2 B2 R2 L2 F2 L2 D B2 F2 D
 
   digitalWrite(dirPinBack, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinBack, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinBack, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, HIGH); //  with clockwise
   for (int x = 0 ; x < 50 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinFront, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinFront, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinFront, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinRight, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
  //   F2 B2 D2 L2 D2 * L2 R2 D2 R2 D2 R2 F2 D' R2 B2 R2 L2 F2 L2 D B2 F2 D
 
   digitalWrite(dirPinFront, HIGH);//   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinFront, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinFront, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinBack, HIGH);//   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinBack, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinBack, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinLeft, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinLeft, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinLeft, LOW);
     delayMicroseconds(1000);
   }  delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 //    L2 R2 D2 R2 * D2 R2 F2 D' R2 B2 R2 L2 F2 L2 D B2 F2 D
 
   digitalWrite(dirPinLeft, HIGH);//   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinLeft, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinLeft, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinRight, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinRight, HIGH);  // with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
 //     D2 R2 F2 D' * R2 B2 R2 L2 F2 L2 D B2 F2 D
 
   digitalWrite(dirPinDown, HIGH);   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinRight, HIGH);  // with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinFront, HIGH);//   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinFront, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinFront, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, LOW);  // with anticlockwise
   for (int x = 0 ; x < 50 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
  //   R2 B2 R2 L2 F2 L2 * D B2 F2 D
 
   digitalWrite(dirPinRight, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinBack, HIGH); //  with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinBack, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinBack, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinRight, HIGH);  // with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinRight, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinRight, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinLeft, HIGH);  // with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinLeft, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinLeft, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinFront, HIGH);  // with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinFront, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinFront, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinLeft, HIGH);  // with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinLeft, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinLeft, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
     D B2 F2 D
   digitalWrite(dirPinDown, HIGH);  // with clockwise
   for (int x = 0 ; x < 50 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinBack, HIGH);//   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinBack, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinBack, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinFront, HIGH);//   with clockwise
   for (int x = 0 ; x < 100 ; x++) {
     digitalWrite(stepPinFront, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinFront, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
 
   digitalWrite(dirPinDown, HIGH);//   with clockwise
   for (int x = 0 ; x < 50 ; x++) {
     digitalWrite(stepPinDown, HIGH);
     delayMicroseconds(1000);
     digitalWrite(stepPinDown, LOW);
     delayMicroseconds(1000);
   }
   delay(500);
   lcdPrint(500);
   Serial.println("end U'");
 }


void lcdPrint(int num) {
  timer += num;

  if (timer == 1000) {
    lcd.clear();
    counter2 += 1;

    if (counter2 == 1) {
      lcd.setCursor(6, 1); //  Set cursor to character 2 on line 0
      lcd.print("Second");
    }
    else if (counter2 > 1 and counter2 < 10) {
      lcd.setCursor(6, 1);  // Set cursor to character 2 on line 0
      lcd.print("Seconds");
    }
    else if (counter2 >= 10 and counter2 < 100) {
      lcd.setCursor(7, 1); //  Set cursor to character 2 on line 0
      lcd.print("Seconds");
    }
    else if (counter2 >= 100) {
      lcd.setCursor(8, 1); //  Set cursor to character 2 on line 0
      lcd.print("Seconds");
    }
    lcd.setCursor(5, 0);  // Set cursor to character 2 on line 0
    lcd.print("Start!");
    lcd.setCursor(4, 1);
    lcd.print(counter2);
         delay(1000);
    timer = 0;
  }
}